﻿using Microsoft.AspNetCore.Authorization;
using SqlSugar;
using TomNet.App.Model.DB;
using TomNet.AspNetCore.Data;
using TomNet.AspNetCore.Mvc;
using TomNet.Data;
using TomNet.SqlSugarCore.Entity;

namespace TomNet.App.WebApi.Controllers
{
    [AllowAnonymous]
    /// <summary>
    /// 动态
    /// </summary>
    public class InformationController : ApiController
    {
        private readonly IUnitOfWork _unitOfWork;

        public InformationController(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }
        /// <summary>
        /// 动态列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public AjaxResult List(int pageIndex = 1, int pageSize = 3)
        {
            //ProductListDto
            int total = 0;
            var data = _unitOfWork.DbContext.Queryable<Information, InformationImages>((m, im) => new object[] {
                JoinType.Left,m.Id==im.InformationId
            }).Select((m, im) => new
            {
                BType = m.Title,
                CreateTime = m.CreateTime,
                Browse = m.Browse,
                Id = m.Id,
                img = im.Address
            }).ToPageList(pageIndex, pageSize, ref total);
            return new AjaxResult("允许访问", AjaxResultType.Success, new { Data = data, Total = total });
        }
        /// <summary>
        /// 动态详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public AjaxResult Info(int Id)
        {
            int total = 0;
            var data = _unitOfWork.DbContext.Queryable<Information, InformationImages>((m, im) => new object[] {
                JoinType.Left,m.Id==im.InformationId
            }).Where((m, im) => m.Id == Id).Select((m, im) => new
            {
                m.Title,
                m.Browse,
                m.Content,
                m.CreateTime,
                m.Id,
                img = im.Address
            }).Single();
            return new AjaxResult("允许访问", AjaxResultType.Success, new { Data = data, Total = total });
        }
    }
}